package com.netjava.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.netjava.dao.IEndAuctionDAO;
import com.netjava.pojo.AucRecord;
import com.netjava.pojo.EndAuction;
import com.netjava.pojo.User;

/**
 * A data access object (DAO) providing persistence and search support for
 * EndAuction entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.netjava.pojo.EndAuction
 * @author MyEclipse Persistence Tools
 */

public class EndAuctionDAO extends HibernateDaoSupport implements IEndAuctionDAO{
	private static final Log log = LogFactory.getLog(EndAuctionDAO.class);
	// property constants
	public static final String END_TIME = "endTime";
	public static final String LAST_PRICE = "lastPrice";
	public static final String OTHER1 = "other1";

	protected void initDao() {
		// do nothing
	}

	public void save(EndAuction EndAuction) {
		log.debug("saving EndAuction instance");
		try {
			getHibernateTemplate().save(EndAuction);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(EndAuction EndAuction) {
		log.debug("deleting EndAuction instance");
		try {
			getHibernateTemplate().delete(EndAuction);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public EndAuction findById(java.lang.Integer id) {
		log.debug("getting EndAuction instance with id: " + id);
		try {
			EndAuction instance = (EndAuction) getHibernateTemplate().get(
					"com.netjava.pojo.EndAuction", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(EndAuction instance) {
		log.debug("finding EndAuction instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding EndAuction instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from EndAuction as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByEndTime(Object endTime) {
		return findByProperty(END_TIME, endTime);
	}

	public List findByLastPrice(Object lastPrice) {
		return findByProperty(LAST_PRICE, lastPrice);
	}

	public List findByOther1(Object other1) {
		return findByProperty(OTHER1, other1);
	}

	public List findAll() {
		log.debug("finding all EndAuction instances");
		try {
			String queryString = "from EndAuction";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public EndAuction merge(EndAuction detachedInstance) {
		log.debug("merging EndAuction instance");
		try {
			EndAuction result = (EndAuction) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(EndAuction instance) {
		log.debug("attaching dirty EndAuction instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(EndAuction instance) {
		log.debug("attaching clean EndAuction instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static EndAuctionDAO getFromApplicationContext(ApplicationContext ctx) {
		return (EndAuctionDAO) ctx.getBean("endAuctionDAO");
	}
	
	
	
	@SuppressWarnings("unchecked")
	public List<EndAuction> findByUserId(final User user){
		List<EndAuction> list=new ArrayList<EndAuction>();
		
		try{
			list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					String queryString="FROM EndAuction AS endauction WHERE endauction.user.userId=? ORDER BY endauction.id DESC";
					Query query=session.createQuery(queryString);
					query.setInteger(0,user.getUserId());
					return query.list();
				}
				
			});
		}catch(RuntimeException re){
			throw re;
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<EndAuction> findAllByTime() {
		List<EndAuction> list=new ArrayList<EndAuction>();
		try{
			list=(List<EndAuction>)this.getHibernateTemplate().execute(new HibernateCallback(){
				public Object doInHibernate(final Session session)
						throws HibernateException, SQLException {
		final String hql="FROM EndAuction endauc ORDER BY endauc.endTime DESC";
		final Query query=session.createQuery(hql);
		return query.list();
				}
				
			});
		}catch(RuntimeException re){
			throw re;
		}
		System.out.println("得到竞拍数："+list.size());
		return list;
}
}